package com.zb.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class MQConfig {


    public static final String SECKILL_EXCHANGE = "seckill_exchange";
    public static final String SECKILL_QUEUE = "seckill_queue";
    public static final String SECKILL_ROUTING = "seckill_routing";

    @Bean(SECKILL_EXCHANGE)
    public Exchange createSeckillExchange() {
        return ExchangeBuilder.topicExchange(SECKILL_EXCHANGE).durable(true).build();
    }

    @Bean(SECKILL_QUEUE)
    public Queue createSeckillQueue() {
        return new Queue(SECKILL_QUEUE, true, false, false, null);
    }

    @Bean
    public Binding bindingSeckillExchangeAndQueue(@Qualifier(SECKILL_EXCHANGE) Exchange exchange, @Qualifier(SECKILL_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(SECKILL_ROUTING).noargs();
    }


    ///////////////////////////////////////////////////////////////////////////

    public static final String ONESECKILL_EXCHANGE = "one_seckill_exchange";
    public static final String ONESECKILL_QUEUE = "one_seckill_queue";
    public static final String ONESECKILL_ROUTING = "one_seckill_routing";

    public static final String TWOSECKILL_EXCHANGE = "two_seckill_exchange";
    public static final String TWOSECKILL_QUEUE = "two_seckill_queue";
    public static final String TWOSECKILL_ROUTING = "two_seckill_routing";

    @Bean(ONESECKILL_EXCHANGE)
    public Exchange oneExchange() {
        return ExchangeBuilder.topicExchange(ONESECKILL_EXCHANGE).durable(true).build();
    }

    @Bean(ONESECKILL_QUEUE)
    public Queue oneQueue() {
        Map<String, Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange", TWOSECKILL_EXCHANGE);
        params.put("x-dead-letter-routing-key", TWOSECKILL_ROUTING);
        return new Queue(ONESECKILL_QUEUE, true, false, false, params);
    }

    @Bean
    public Binding oneBinding(@Qualifier(ONESECKILL_EXCHANGE) Exchange exchange, @Qualifier(ONESECKILL_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(ONESECKILL_ROUTING).noargs();
    }


    @Bean(TWOSECKILL_EXCHANGE)
    public Exchange twoExchange() {
        return ExchangeBuilder.topicExchange(TWOSECKILL_EXCHANGE).durable(true).build();
    }

    @Bean(TWOSECKILL_QUEUE)
    public Queue twoQueue() {
        return new Queue(TWOSECKILL_QUEUE, true, false, false, null);
    }

    @Bean
    public Binding twoBinding(@Qualifier(TWOSECKILL_EXCHANGE) Exchange exchange, @Qualifier(TWOSECKILL_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(TWOSECKILL_ROUTING).noargs();
    }

}
